<template>
	<view>
		<view class="swiperPanel" @touchstart="startMove" @touchend="endMove">
			<view class="swiperItem" v-for="(item, index) in swiperList" :key="index"
				:style="{transform: itemStyle[index].transform, zIndex: itemStyle[index].zIndex, opacity: itemStyle[index].opacity}">
				<view class="children" @click="routerTo(item)">
					<image class="pic" :src="item.url"></image>
					<!-- <text class="name">{{item.name}}</text> -->
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			swiperList: {
				type: Array,
				default: [
					{
						url: 'https://img.shihhl.com/jinye/home_7.png',
						name: '这是一个图片'
					}
				]
			},
			// 自动翻页 间隔2秒
			timeNum:{
				type:Number,
				default:4000
			},
			// 点击后 5秒内未操作 自动翻页开启
			interval:{
				type:Number,
				default:5000
			},
		},
		data() {
			return {
				slideNote: {
					x: 0,
					y: 0
				},
				screenWidth: 0,
				itemStyle: [],
				timer: null,
				timer1: null
			};
		},
		watch: {
			swiperList: {
				handler(val) {
					if (val.length) {
						var macInfo = uni.getSystemInfoSync();
						this.screenWidth = macInfo.screenWidth;
						// 计算swiper样式
						this.swiperList.forEach((item, index) => {
							this.itemStyle.push(this.getStyle(index))
						})
					}
				},
				deep: true,
				immediate: true
			}
		},
		mounted() {
			this.doSomething()
		},
		onUnload() {
			this.timer = null
			this.timer1 = null
		},
		beforeDestroy() {
			this.timer = null
			this.timer1 = null
		},
		methods: {
			doSomething() {
				this.$nextTick(() => {
					this.timer = setInterval(() => {
						var newList = JSON.parse(JSON.stringify(this.itemStyle))
						// 向左滑动
						var last = [newList.pop()]
						newList = last.concat(newList)
						this.itemStyle = newList
					}, this.timeNum)
				})
			},
			routerTo(data) {
				// 此处为点击逻辑
				// 或者给父组件抛出事件
				// this.$emit("方法名字",参数)
			},
			getStyle(e) {
				console.log(e , e > this.swiperList.length / 2);
				if (e > this.swiperList.length / 2) {
					var right = this.swiperList.length - e
					return {
						transform: 'scale(' + (0.7) + ') translate(-' + (right * 44) + '%,0px)',
						zIndex: 100 - right,
						opacity: 0.8 / right
					}
				} else {
					console.log(e)
					if(e == 1){
						return {
							transform: 'scale(' + (0.7) + ') translate(' + (e * 44) + '%,0px)',
							zIndex: 100 - e,
							opacity: 0.8 / e
						}
					}else{
						return {
							transform: 'scale(' + (1 - e / 10) + ') translate(' + (e * 26) + '%,0px)',
							zIndex: 100 - e,
							opacity: 0.8 / e
						}
					}
				}
			},
			startMove(e) {
				clearInterval(this.timer)
				this.timer = null
				this.slideNote.x = e.changedTouches[0] ? e.changedTouches[0].pageX : 0;
				this.slideNote.y = e.changedTouches[0] ? e.changedTouches[0].pageY : 0;
			},
			endMove(e) {
				var newList = JSON.parse(JSON.stringify(this.itemStyle))
				if ((e.changedTouches[0].pageX - this.slideNote.x) < -10) {
					// 向左滑动
					var last = [newList.pop()]
					newList = last.concat(newList)
				} else if ((e.changedTouches[0].pageX - this.slideNote.x) >= 10) {
					// 向右滑动
					newList.push(newList[0])
					newList.splice(0, 1)
				}
				this.itemStyle = newList
				// 清除之前的定时器，以防多次点击
				clearTimeout(this.timer1);

				// 设置定时器，5秒后执行doSomething方法
				this.timer1 = setTimeout(this.doSomething, this.interval);
			}
		}
	}
</script>

<style lang="scss">
	.swiperPanel {
		margin: 20rpx 0;
		height: 600rpx;
		width: 100%;
		overflow: hidden;
		position: relative;

		.swiperItem {
			height: 100%;
			width: 100%;
			position: absolute;
			top: 0;
			left: 0;
			transition: all .5s;

			.children {
				height: 100%;
				width: 400rpx;
				margin: 2rpx auto;
				position: relative;

				.pic {
					height: 100%;
					width: 100%;
					border-radius: 20px;
					// box-shadow: 0 0 10px #333;
				}

				.name {
					position: absolute;
					width: 100%;
					bottom: 0;
					left: 0;
					background: rgba(0, 0, 0, 0.5);
					box-shadow: 0rpx 4rpx 21rpx 0rpx rgba(0, 0, 0, 0.07);
					border-radius: 0 0 20px 20px;
					height: 85rpx;
					line-height: 85rpx;
					font-family: Source Han Sans SC, Source Han Sans SC;
					font-weight: 400;
					font-size: 32rpx;
					color: #FFFFFF;
					text-align: center;
					font-style: normal;
					text-transform: none;
				}
			}
		}
	}
</style>
